﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KerriganDataContract;
using KerriganDataContract.Forum;
using KerriganDataContract.Person;

namespace KerriganForum
{
    public partial class Message : BasePage
    {
        private string requestCookie;
        private int ID
        {
            get
            {
                if (ViewState["id"] == null)
                    ViewState["id"] = -1;
                return (int)ViewState["id"];
            }
            set
            {
                ViewState["id"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                using (ServiceProxy Srv = new ServiceProxy(Request.Cookies))
                {

 

                    try
                    {

                        requestCookie = Request.Cookies["BXPXAUTHFWCF"].Value;

                    }
                    catch (Exception ex)
                    {
                        requestCookie = null;
                    }

                    if (Identify.Anonymous == null || requestCookie == null || Identify.IdUser == 0)
                    {
                        Identify.CheckUser(requestCookie);
                    }
                    else if (requestCookie != null && Identify.Anonymous == true)
                    {
                        Identify.CheckUser(requestCookie);
                    }

                }


                ID = Convert.ToInt32(Request.QueryString["id"]);

                using (ServiceProxy Srv = new ServiceProxy())
                {
                    GridView1.DataSource = Srv.Service.GetMessageToDisplay(ID);
                    GridView1.DataBind();
                    Srv.Service.UpdateViews(ID);
                }

                buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
                btn_add_Click(sender, e);
                
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {

            RefreshDataGrid();
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
            btnAddMessage.Attributes.Add("onClick", "ShowNewModal();");
        }


        protected void RefreshDataGrid()
        {
            this.GridView1.DataSource = null;
            using (ServiceProxy Srv = new ServiceProxy())
            {

                GridView1.DataSource = Srv.Service.GetMessageToDisplay(ID);
                GridView1.DataBind();

                buttonNoUse.Click += new EventHandler(this.btnSearch_Click);

            }

        }

        protected void CheckWebUI()
        {

            if (Identify.TypeUser == 1)
            {

                //(e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + article.IdBlogArticle + "');");
                //(e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = article.IdBlogArticle.ToString();
            }
            else
            {


            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                using (ServiceProxy Srv = new ServiceProxy())
                {
                    vwGetForumMessage message = e.Row.DataItem as vwGetForumMessage;

                    if (Identify.Anonymous != true)
                    {


                                if (message != null)
                                {

                                    if (message.Person == Identify.IdUser || Identify.TypeUser == 1)
                                    {

                                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = true;
                                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = true;
                                        btnAddMessage.Visible = true;
                                        (e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + message.IdForumMessage + "');");
                                        (e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = message.IdForumMessage.ToString();

                                    }
                                    else
                                    {
                                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;
                                        btnAddMessage.Visible = true;
                                    }
                                }
  

                    }
                    else
                    {

                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;
                        btnAddMessage.Visible = false;
                    }

                }
               
            }

        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyDelete")
            {
                using (ServiceProxy Srv = new ServiceProxy())
                {
                    Boolean result;
                    int id = Convert.ToInt32(e.CommandArgument);
                    result = Srv.Service.DeleteMessage(id);
                    if (result == true)
                    {
                        Response.Redirect("~/Home.aspx");
                    }
                    else
                    {
                        RefreshDataGrid();
                    }
                }
            }
        }

        protected void GridView1_Paging(object sender, GridViewPageEventArgs e)
        {

            GridView1.PageIndex = e.NewPageIndex;
            RefreshDataGrid();
        }

        protected void btnOkay_Click(object sender, EventArgs e)
        {


            try
            {
                AddMessage();
                ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);

            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "onload", "onError();", true);
            }
        }



        private void AddMessage()
        {
            using (ServiceProxy Srv = new ServiceProxy())
            {
                ForumMessageDC bo = new ForumMessageDC();
                bo.Message = txtNameMessage.InnerText;
                bo.Topic = new ForumTopicDC();
                bo.Topic.IdTopic = ID;
                bo.Person = new PersonDC();
                bo.Person.IdPerson = Identify.IdUser;
                bo.Date = DateTime.Now;
                bo.MainMessage = false;
                Srv.Service.AddMessage(bo);


            }

        }
    }
}